using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;

namespace Stratman.DesktopProcess.Ec2Console
{
    /// <summary>
    /// Allows the user to enter values for the various options associated with this application
    /// such as the X509 certificate to use during requests, paths to the SSH and SFTP client
    /// applications.
    /// </summary>
    public partial class OptionsForm : Form
    {
        /// <summary>
        /// Default constructor; binds a list of certificates in the current user's local store to
        /// the certificate dropdown.
        /// </summary>
        public OptionsForm()
        {
            InitializeComponent();

            ArrayList listItems = new ArrayList();
            X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            // Open a handle to the current user's personal certificate store
            certificateStore.Open(OpenFlags.ReadOnly);

            // Add each certificate to the selection box
            foreach (X509Certificate2 certificate in certificateStore.Certificates)
                listItems.Add(new DictionaryEntry(certificate.GetCertHashString(), certificate.GetNameInfo(X509NameType.SimpleName, false)));

            // Bind the dropdown to the certificate list
            awsCertificateDropdown.DataSource = listItems;
            awsCertificateDropdown.DisplayMember = "Value";
            awsCertificateDropdown.ValueMember = "Key";
        }

        /// <summary>
        /// Directory in which the key files corresponding to the user's keys are stored.
        /// </summary>
        public string KeypairsDirectory
        {
            get
            {
                return keypairsDirectoryTextBox.Text;
            }

            set
            {
                keypairsDirectoryTextBox.Text = value;
            }
        }

        /// <summary>
        /// Name of the certificate to use during requests to the EC2 web service interface.
        /// </summary>
        public string AwsCertificate
        {
            get
            {
                return awsCertificateDropdown.Text;
            }

            set
            {
                awsCertificateDropdown.Text = value;
            }
        }

        /// <summary>
        /// Path to the SSH client application.
        /// </summary>
        public string PathToSSH
        {
            get
            {
                return pathToSSHTextBox.Text;
            }

            set
            {
                pathToSSHTextBox.Text = value;
            }
        }

        /// <summary>
        /// Path to the SFTP client application.
        /// </summary>
        public string PathToSFTP
        {
            get
            {
                return pathToSFTPTextBox.Text;
            }

            set
            {
                pathToSFTPTextBox.Text = value;
            }
        }

        /// <summary>
        /// Event handler that is invoked when the browse directory button next to the keypair
        /// directory text box is clicked; allows the user to choose a directory.
        /// </summary>
        /// <param name="sender">
        /// Object from which this event originated.
        /// </param>
        /// <param name="e">
        /// Arguments associated with this event.
        /// </param>
        private void keypairsDirectoryBrowseButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(keypairsDirectoryTextBox.Text))
                folderBrowserDialog.SelectedPath = keypairsDirectoryTextBox.Text;

            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
                keypairsDirectoryTextBox.Text = folderBrowserDialog.SelectedPath;
        }

        /// <summary>
        /// Event handler that is invoked when the user clicks the OK button; validates that the
        /// necessary form fields have been filled in prior to closing the form.
        /// </summary>
        /// <param name="sender">
        /// Object from which this event originated.
        /// </param>
        /// <param name="e">
        /// Arguments associated with this event.
        /// </param>
        private void okButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(awsCertificateDropdown.Text))
            {
                MessageBox.Show("You must specify the AWS certificate.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (String.IsNullOrEmpty(keypairsDirectoryTextBox.Text))
            {
                MessageBox.Show("You must specify the keypairs directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Event handler that is invoked when the user clicks the cancel button; sets the
        /// DialogResult property appropriately and then closes the form.
        /// </summary>
        /// <param name="sender">
        /// Object from which this event originated.
        /// </param>
        /// <param name="e">
        /// Arguments associated with this event.
        /// </param>
        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// Event handler that is invoked when the browse directory button next to the SSH path
        /// text box is clicked; allows the user to choose the path to the application executable.
        /// </summary>
        /// <param name="sender">
        /// Object from which this event originated.
        /// </param>
        /// <param name="e">
        /// Arguments associated with this event.
        /// </param>
        private void pathToSSHBrowseButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(pathToSSHTextBox.Text))
                folderBrowserDialog.SelectedPath = pathToSSHTextBox.Text;

            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
                pathToSSHTextBox.Text = folderBrowserDialog.SelectedPath;
        }

        /// <summary>
        /// Event handler that is invoked when the browse directory button next to the SFTP path
        /// text box is clicked; allows the user to choose the path to the application executable.
        /// </summary>
        /// <param name="sender">
        /// Object from which this event originated.
        /// </param>
        /// <param name="e">
        /// Arguments associated with this event.
        /// </param>
        private void pathToSFTPBrowseButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(pathToSFTPTextBox.Text))
                folderBrowserDialog.SelectedPath = pathToSFTPTextBox.Text;

            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
                pathToSFTPTextBox.Text = folderBrowserDialog.SelectedPath;
        }
    }
}